﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebDomain.Concrete;
using WebDomain.Entities;

namespace CZTopComputers.Controllers
{
    [Authorize]
    public class AdminController : Controller
    {
        EFProductRepository repository = new EFProductRepository();
        public AdminController()
        {
        }

        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Products()
        {
            return View(repository.Products);
        }
        public RedirectResult ChangeQty(String button, int ID)
        {
            var product = repository.Products.First(p => p.ID == ID);
            switch (button)
            {
                case "+":
                    product.Quantity++;
                    break;
                case "-":
                    product.Quantity--;
                    break;
            }
            repository.SaveProduct(product);
            return Redirect("~/Admin/Products");
        }
        public ViewResult Create()
        {
            return View();
        }
        [HttpPost]
        public ActionResult Create(Product product, HttpPostedFileBase image)
        {
            if (image != null)
            {
                product.ImageMimeType = image.ContentType;
                BinaryReader reader = new BinaryReader(image.InputStream);
                product.ImageData = reader.ReadBytes(image.ContentLength);
                reader.Close();
                image.InputStream.Read(product.ImageData, 0, image.ContentLength);
            }
            repository.SaveProduct(product);
            TempData["message"] = string.Format("{0} has been saved", product.Name);
            return RedirectToAction("Products");

        }
        [HttpPost]
        public ActionResult Edit(Product product, HttpPostedFileBase image)
        {
            if (ModelState.IsValid)
            {
                if (image != null)
                {
                    product.ImageMimeType = image.ContentType;
                    BinaryReader reader = new BinaryReader(image.InputStream);
                    product.ImageData = reader.ReadBytes(image.ContentLength);
                    reader.Close();
                    image.InputStream.Read(product.ImageData, 0, image.ContentLength);
                }
                repository.SaveProduct(product);
                TempData["message"] = string.Format("{0} has been saved", product.Name);
                return RedirectToAction("Products");
            }
            else
            {
                return View(product);
            }
        }

        public ViewResult Edit(int ID)
        {
            Product product = repository.Products.FirstOrDefault(p => p.ID == ID);
            return View(product);
        }

        public ActionResult Delete(int ID)
        {
            var temp = repository.Products.FirstOrDefault(p => p.ID == ID);
            TempData["message"] = string.Format("{0} has been deleted", temp.Name);
            repository.DeleteProduct(temp);

            return RedirectToAction("Products");
        }
    }
}
